<?php use model\userModel;
    use Controller\Controller;

    class usersController extends Controller {
        private $USER;

        function __construct() {
            session_start();
            $this->USER = new userModel();
        }

        public function indexAction() {

        }

        public function getAction($params = false) {
            if (!isset($_SESSION['admin'])){
                $this->errorJsonDie(403, "unauthorized");
            }
            $response = [];
            if ($this->request()["method"] !== "GET") {
                $this->errorJsonDie(400);
            }
            if (!$params) {
                $response = $this->USER->getAllUsers();
            } else {
                $response = $this->USER->getUserBy($params);
            }
            if ($response["status"] == 0){
                $this->errorJsonDie(404, "Not Found");
            }
            $this->successJson($response["response"]);
        }

        public function smsCodeAction() {
            $request = $this->request();
            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            $code = rand(1000, 9999);
            $phone = (isset($request["json"]["phone"])) ? $phone = $request["json"]["phone"] : $this->errorJsonDie(400, "Phone is requared");

            $_SESSION['user'] = [
                "phone" => $phone,
                "code" => $code
            ];

            $this->successJson($_SESSION['user']['code']);
        }

        public function registrationAction() {
            $response = [];
            $request = $this->request();

            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($request["json"]["phone"]) || !isset($request['json']['code'])){
                $this->errorJsonDie(400);
            }
//            $user = (isset($_SESSION['user']))? $user = $_SESSION['user'] : $this->errorJsonDie(403);
//            if ($user['phone'] != $request['json']['phone'] || $user['code'] != $request['json']['code']){
//                $this->errorJsonDie(404, "Not Found");
//            }
            $response = $this->USER->addUser($request['json']['phone']);
//            unset($_SESSION['user']);
            $this->successJson($response["response"]);
        }

        public function infoAction() {
            $request = $this->request();
            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($request['json']['token'])){
                $this->errorJsonDie(403);
            }
            $response = $this->USER->checkUserToken($request['json']['token']);
            if ($response['status'] == 0){
                $this->errorJsonDie(400);
            }
            $this->successJson($response['response']);
        }

        public function ordersAction() {
            $request = $this->request();
            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($request['json']['token'])){
                $this->errorJsonDie(403);
            }
            $token = $this->USER->checkUserToken($request['json']['token']);
            if ($token['status'] == 0) {
                $this->errorJsonDie(403);
            }
            $response = $this->USER->getAllUserOrders($token['response']['token']);
            if ($response['status'] == 0){
                $this->errorJsonDie(400);
            }
            $this->successJson($response['response']);

        }

        public function eventsAction() {
            $request = $this->request();
            if ($request["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($request['json']['token'])){
                $this->errorJsonDie(403);
            }
            $token = $this->USER->checkUserToken($request['json']['token']);
            if ($token['status'] == 0) {
                $this->errorJsonDie(403);
            }
            $response = $this->USER->getAllUserEvents($token['response']['token']);
            if ($response['status'] == 0){
                $this->errorJsonDie(400);
            }
            $this->successJson($response['response']);

        }

        public function editAction() {
            $request = $this->request();
            $json = $request['json'];
            if ($request["method"] !== "PATCH") {
                $this->errorJsonDie(404);
            }
            if (!isset($json["id"])
                || !isset($json["phone"])
                || !isset($json["age"])
                || !isset($json["name"])
                || !isset($json["email"])
                || !isset($json["token"])
            ){
                $this->errorJsonDie(400, "mismatch params");
            }
            $token = $this->USER->checkUserToken($json['token']);
            if ($token['status'] == 0) {
                $this->errorJsonDie(403);
            }
            $response = $this->USER->editUser($json["id"], $json["phone"], $json["age"], $json["name"], $json["email"]) ;
            if ($response['status'] == 0){
                $this->errorJsonDie(400);
            }
            $this->successJson($response['response']);

        }

    }